#ifndef LOCKFREE_SKIPLIST_H
#define LOCKFREE_SKIPLIST_H

#include "../atomic/tagged_ptr.h"
#include <stddef.h>
#include <stdbool.h>

#define MAX_LEVEL 32
#define PROBABILITY 0.25

// 跳表节点
typedef struct skiplist_node {
    int key;
    void* value;
    int level;
    atomic_tagged_ptr_t next[]; // 柔性数组
} skiplist_node_t;

// 跳表
typedef struct {
    skiplist_node_t* head;
    atomic_int level;
    int (*compare)(const void* key1, const void* key2);
} skiplist_t;

// 初始化跳表
bool skiplist_init(skiplist_t* list, 
                  int (*compare)(const void* key1, const void* key2));

// 销毁跳表
void skiplist_destroy(skiplist_t* list);

// 插入键值对
bool skiplist_insert(skiplist_t* list, int key, void* value);

// 查找值
void* skiplist_find(skiplist_t* list, int key);

// 删除键值对
bool skiplist_remove(skiplist_t* list, int key);

// 获取大小
size_t skiplist_size(skiplist_t* list);

#endif // LOCKFREE_SKIPLIST_H 